|
|
|
|
|
|
|
(table continued from previous page) |
|
|
|
|
| Controls | Properties | Settings | | Left | 120 | | Top | 2222 | | Width | 5835 |
|
|
|
|
|
|
It would be nice to say that you could build a Subform without using any Visual Basic 5 code, but that's not quite true. You need just over 10 lines of code to get your data grid at the bottom of the form linked to the master table at the top of the form. Place the code in Listing 12.25 in the Data1_Reposition event of the Publishers table data control. |
|
|
|
|
|
|
|
|
Listing 12.25. Code to update the Subform with the Reposition event. |
|
|
|
|
|
|
|
|
Private Sub Data1_Reposition()
'
Dim strSQL As String
Dim strKey As String
'
' create select to load grid
If Text1(0).Text = "" Then
strKey = "0"
Else
strKey = Trim(Text1(0).Text)
End If
'
strSQL = "SELECT ISBN,Title,YearPub FROM Titles WHERE PubID=" & strKey
Data2.RecordSource = strSQL ' load grid-bound data control
Data2.Refresh ' refresh data control
DBGrid1.ReBind ' refresh grid
'
End Sub |
|
|
|
|
|
|
|
|
The preceding code is used to create a new SQL SELECT statement using the PubID value of the Publishers table. This SQL statement is used to generate a new data set for the Data2 data control. This is the control that supplies the data grid. Once the new record source has been created, invoke the Refresh method to update the data control and the ReBind method to update the data grid. That's it; there are only eleven lines of Visual Basic code, including the comments. Now save the form as SUBFORM.FRM and the project as SUBFORM.VBP, and run the program. When the form loads, you see the first record in the Header table displayed at the top of the form, and a list of all the outstanding invoices for that customer in the grid at the bottom of the form (see Figure 12.23). |
|
|
|
|
|